package com.evil.kingdom.pojo.dto.alive;

import com.alibaba.fastjson.JSON;
import com.evil.kingdom.pojo.enums.socket.MessageTypeEnum;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 长连接消息
 *
 * @author liyang
 * @date 2024-06-01 13:16
 */
@Getter
@Setter
@NoArgsConstructor
public class AliveMessage<T extends AliveMessageData> {

    /**
     * 消息类型不能为空
     */
    @NotNull(message = "")
    private MessageTypeEnum type;

    /**
     * 消息实体
     */
    private T t;

    public AliveMessage(T t) {
        this.type = MessageTypeEnum.findByMessageClass(t.getClass());
        this.t = t;
    }

    public String toMessageStr() {
        return JSON.toJSONString(this);
    }
}
